﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;

/* C# Symmetric key encryption (also known as secret-key encryption), is a cryptographic technique that uses a single secret key to encrypt and decrypt data. 
 * The methods within symmetric encryption are also known as “ciphers”, and process plain text using the secret key to generate encrypted data, called “cipher text”.
 * 
 * Symmetric algorithms are fast and are convenient when encrypting large amounts of data. 
 * Decrypting the data without the key can take an attacker hundreds of years (if a proper key length is used).
 * 
 * The disadvantage of this technique is that the secret key must be available to both the process that encrypts the data and the process that decrypts the data. 
 * This is a challenging problem because the key itself cannot be protected with symmetric encryption.
 * The secure way to exchange the symmetric key is to encrypt it asymmetrically.
 */
namespace VMCCryptoLib.SymmetricEncryption
{
    public class TripleDESEncryption
    {
    	#region --- Encrypt and Decrypt a string
    	public static string EncryptString(string inputData, string password)
    	{
    		string rs = string.Empty;
    		return rs;
    	}
    	
    	public static string DecryptString(string cipherData, string password)
    	{
    		string rs = string.Empty;
    		return rs;
    	}
    	#endregion 
    	
    	#region --- Encrypt and Decrypt a content of file and save it into another file
    	#endregion
    	
    	#region --- Private Methods
    	private static byte[] TripleDESEncrypt(byte[] clearData, byte[]Key, byte[] IV)
    	{
    		MemoryStream memoStream = new MemoryStream();
    		TripleDES algo = TripleDES.Create();
    		algo.Key=Key;
    		algo.IV =IV;
    		CryptoStream crpStream = new CryptoStream(memoStream,algo.CreateEncryptor(), CryptoStreamMode.Write);
    		crpStream.Write(clearData,0,clearData.Length);
    		crpStream.Close();
    		byte[] encryptedData = memoStream.ToArray();
    		return encryptedData;
    	}
    	
    	private static byte[] TriplDESDecrypt(byte[] cipherData, byte[]Key, byte[] IV)
    	{
    		MemoryStream memoStream = new MemoryStream();
    		TripleDES algo = TripleDES.Create();
    		algo.Key=Key;
    		algo.IV=IV;
    		CryptoStream crpStream = new CryptoStream(memoStream,algo.CreateDecryptor(), CryptoStreamMode.Write);
    		crpStream.Write(cipherData,0,cipherData.Length);
    		crpStream.Close();
    		byte[] decryptedData = memoStream.ToArray();
    		return decryptedData;
    	}
    	#endregion
    }
}
